<?php
function save2bet2( $uid, $acl, $pid, $btype, $order, $num, $money, $rate, $u2, $u3, $u4, $u5, $stime )
{
    global $lotto_id;
    global $prot_start;
    global $raters;
    $subid_count = intval( $order / 10 );
    $fields = array();
    $fields['user_id'] = $uid;
    $fields['prev_id'] = $pid;
    $fields['acl'] = $acl;
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $btype;
    $fields['numbers'] = "'{$order}|{$num}'";
    $fields['amount'] = $money;
    $rate = 99;
    $l = 0;
    while ( $l < count( $num ) )
    {
        $rate = $raters->f["r".$num[$l]] < $rate ? $raters->f["r".$num[$l]] : $rate;
        ++$l;
    }
    if ( $rate == 99 || $rate <= 0 )
    {
        err( "注单存入失败, 赔率值错误！" );
    }
    $fields['rate'] = $rate;
    $fields['line_nums'] = "'{$num}'";
    $fields['unitmoney'] = $money;
    if ( $fields['rate'] <= 0 || $fields['amount'] <= 0 )
    {
        return 0 - 1;
    }
    $percent_sw = $u5['percent_sw'];
    $protmod = $u5['protmod'];
    $bttt = $btype.$subid_count;
    $rate0_8num = 0;
    if ( $u5['reat0_8_open'] == 1 )
    {
        $rate0_8num = 0.04;
    }
    if ( $acl == 2 )
    {
        $fields['uperid3'] = $u2['prev_id'];
        $fields['uperid4'] = $u3['prev_id'];
        $fields['uperid5'] = $u4['prev_id'];
        $fields['back2'] = GetBack( $u2['id'], $bttt, 1, 2 );
        $fields['back3'] = GetBack( $u3['id'], $bttt, 1, 3 );
        $fields['back4'] = GetBack( $u4['id'], $bttt, 1, 4 );
        $fields['back5'] = GetBack( $u5['id'], $bttt, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bttt, 1, 255 );
        $fields['percent3'] = $u2['percent0'];
        $fields['percent4'] = $u3['percent0'];
        if ( $percent_sw == 1 )
        {
            $fields['percent5'] = $u5['mpercent'] - $fields['percent4'] - $fields['percent3'];
        }
        else
        {
            $fields['percent5'] = $u4['percent0'];
        }
        $fields['r2'] = $money * $fields['percent3'] / 100;
        $fields['r3'] = $money * $fields['percent4'] / 100;
        $fields['r4'] = $money * $fields['percent5'] / 100;
    }
    else if ( $acl == 3 )
    {
        $fields['uperid3'] = $uid;
        $fields['uperid4'] = $u3['prev_id'];
        $fields['uperid5'] = $u4['prev_id'];
        $fields['back2'] = 100;
        $fields['back3'] = GetBack( $u3['id'], $bttt, 1, 3 );
        $fields['back4'] = GetBack( $u4['id'], $bttt, 1, 4 );
        $fields['back5'] = GetBack( $u5['id'], $bttt, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bttt, 1, 255 );
        $fields['percent3'] = 0;
        $fields['percent4'] = $u3['percent0'];
        if ( $percent_sw == 1 )
        {
            $fields['percent5'] = $u5['mpercent'] - $fields['percent4'];
        }
        else
        {
            $fields['percent5'] = $u4['percent0'];
        }
        $fields['r3'] = $money * $fields['percent4'] / 100;
        $fields['r4'] = $money * $fields['percent5'] / 100;
    }
    else if ( $acl == 4 )
    {
        $fields['uperid4'] = $uid;
        $fields['uperid5'] = $u4['prev_id'];
        $fields['back2'] = 100;
        $fields['back3'] = 100;
        $fields['back4'] = GetBack( $u4['id'], $bttt, 1, 4 );
        $fields['back5'] = GetBack( $u5['id'], $bttt, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bttt, 1, 255 );
        $fields['percent3'] = 0;
        $fields['percent4'] = 0;
        if ( $percent_sw == 1 )
        {
            $fields['percent5'] = $u5['mpercent'];
        }
        else
        {
            $fields['percent5'] = $u4['percent0'];
        }
        $fields['r4'] = $money * $fields['percent5'] / 100;
    }
    else if ( $acl == 5 )
    {
        $fields['uperid5'] = $uid;
        $fields['back2'] = 100;
        $fields['back3'] = 100;
        $fields['back4'] = 100;
        $fields['back5'] = GetBack( $u5['id'], $bttt, 1, 5 );
        $fields['back6'] = GetBack( $u5['supe_account'], $bttt, 1, 255 );
        $fields['percent3'] = 0;
        $fields['percent4'] = 0;
        $fields['percent5'] = 0;
    }
    if ( $percent_sw == 1 )
    {
        $fields['percent255'] = 100 - $u5['mpercent'];
    }
    else
    {
        $fields['percent255'] = 100 - $fields['percent5'] - $fields['percent4'] - $fields['percent3'];
    }
    $fields['r6'] = $money * $fields['percent255'] / 100;
    $fields['betmode'] = 1;
    $fields['ip'] = "'".getIP( )."'";
    db_insert( "bet2", $fields );
    if ( $prot_start == 1 && $protmod == 1 )
    {
        if ( $acl == 2 )
        {
            auto_prots( 3, $btype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $order );
            auto_prots( 4, $btype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $order );
            auto_prots( 5, $btype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $order );
        }
        else
        {
            if ( $acl == 3 )
            {
                auto_prots( 4, $btype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $order );
                auto_prots( 5, $btype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $order );
            }
            else
            {
                if ( $acl == 4 )
                {
                    auto_prots( 5, $btype, $num, $rate, $u2, $u3, $u4, $u5, $stime, $order );
                }
            }
        }
    }
}

echo "  ";
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
global $curruser;
$ui = new UI();
if ( $curruser['acl'] == 5 && $curruser['percent_sw'] == 1 )
{
    err( "你是固定占成的大股东，不能走飞！" );
}
$bettype = 36;
$conf = new Config();
$startbet = $conf->get( "startbet" );
$stopbet = $conf->get( "waitbet" );
$prot_start = $conf->get( "protect_start" );
$betmin = $conf->get( "betmin" );
$subid = intval( $_POST['subid'] );
$subid_count = intval( $subid / 10 );
$money = intval( $_POST['b63'] );
$subid_Arr = array( 21, 31, 41, 51 );
if ( !in_array( $subid, $subid_Arr ) || $money == 0 )
{
    err( "错误的参数！" );
}
$maxnts = $_POST["maxnt".$subid];
$arnum = array();
$numcount = 0;
$i = 0;
while ( $i < 10 )
{
    if ( isset( $_POST["num_".$i] ) )
    {
        $arnum[$numcount] = $i;
        ++$numcount;
    }
    ++$i;
}
if ( $numcount < $subid_count )
{
    err( "必须选择".$subid_count."个尾数！" );
}
else if ( $subid_count < $numcount )
{
    err( "只能选择".$subid_count."个尾数！" );
}
if ( $curruser['enabled'] == 0 || $curruser['enabled'] == 2 )
{
    err( "帐号已被暂停使用！" );
}
$userlist = GetUserList( $curruser['id'] );
if ( $userlist == "" )
{
    $ui->showErrorPage( "<p>还没有建立会员！</p>" );
    exit();
}
$rs = getdb( "select * from lotto where open>='".date( "Y-m-d" )."' order by open" );
if ( $rs->eof )
{
    err( "目前没有可以下注期数！" );
}
$open = $rs->f['open'];
$lotto_id = $rs->f['id'];
$lotto_ns = $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'] + $rs->f['n4'] + $rs->f['n5'] + $rs->f['n6'];
if ( date( "Y-m-d" ) == $open )
{
    if ( date( "Hi" ) < $startbet )
    {
        err( "本期尚未开盘！" );
    }
    if ( $stopbet <= date( "Hi" ) || 0 < $lotto_ns )
    {
        err( "已经截止下注！" );
    }
}
else
{
    err( "本期尚未开盘！" );
}
if ( $prot_start == 0 )
{
    err( "尚未开放走飞补货功能！" );
}
$userid = $curruser['id'];
$acl = $curruser['acl'];
$previd = $curruser['prev_id'];
$rs = getdb( "select sum(amount) from bet2 where user_id=".$userid." and lotto_id=".$lotto_id." and rame=0" );
$usedamount = $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$users = GetUserList( $userid );
if ( $users == "" )
{
    err( "没有会员！" );
}
if ( $acl == 2 )
{
    $u2 = get_user( $userid );
    $u3 = get_user( $u2['prev_id'] );
    $u4 = get_user( $u3['prev_id'] );
    $u5 = get_user( $u4['prev_id'] );
}
else if ( $acl == 3 )
{
    $u2 = "";
    $u3 = get_user( $userid );
    $u4 = get_user( $u3['prev_id'] );
    $u5 = get_user( $u4['prev_id'] );
}
else if ( $acl == 4 )
{
    $u2 = "";
    $u3 = "";
    $u4 = get_user( $userid );
    $u5 = get_user( $u4['prev_id'] );
}
else if ( $acl == 5 )
{
    $u2 = "";
    $u3 = "";
    $u4 = "";
    $u5 = get_user( $userid );
}
$rs = getdb( "select SUM(amount_6h) from `k_user_bet` where user_id in ({$users}) and lotto_id={$lotto_id}" );
$usedamount += $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$rs = getdb( "select * from amounts where bettype=36 and subid ={$subid_count} and user_id=".$userid );
$amount1 = $rs->f['amount1'];
$amount2 = $rs->f['amount2'];
$rs->close();
$err = 0;
$j = 0;
while ( $j < $subid_count - 1 )
{
    $cz = $arnum[$j]."";
    if ( $cz == "" )
    {
        ++$err;
        break;
    }
    $i = $j + 1;
    while ( $i < $subid_count )
    {
        switch ( $arnum[$i] )
        {
            case 0 :
                break;
            case 1 :
                break;
            case 2 :
                break;
            case 3 :
                break;
            case 4 :
                break;
            case 5 :
                break;
            case 6 :
                break;
            case 7 :
                break;
            case 8 :
                break;
            case 9 :
        }
        if ( $arnum[$i] == $cz )
        {
            ++$err;
            break;
        }
        break;
        ++$err;
        break;
        if ( 0 < $err )
        {
            break;
        }
        ++$i;
    }
    ++$j;
}
if ( 0 < $err || $money < 0 )
{
    write_exceptional_log( $curruser['id'], $curruser['acl'], $lotto_id, "使用不正常方式走飞下注（".implode( ",", $arnum )."）money=".$money );
    err( "请勿以非正常方式下注！你的 IP 已经被记录并且也通报你的上层管理员了！" );
}
asort( $arnum );
reset( $arnum );
$num = implode( ",", $arnum );
$numbers = $subid."|".$num;
if ( $acl == 2 )
{
    $rs = getdb( "select sum(amount*mpercent/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and bettype=36 and numbers='".$numbers."' and rame=0" );
}
else if ( $acl == 3 )
{
    $rs = getdb( "select sum(amount*percent2/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and bettype=36 and numbers='".$numbers."' and rame=0" );
    $rs1 = getdb( "select sum(amount*percent".$acl."/100) from bet2 where lotto_id=".$lotto_id." and uperid".$acl."=".$userid." and bettype=36 and numbers='".$numbers."' and rame=0" );
}
else if ( $acl == 4 )
{
    $rs = getdb( "select sum(amount*percent3/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and bettype=36 and numbers='".$numbers."' and rame=0" );
    $rs1 = getdb( "select sum(amount*percent".$acl."/100) from bet2 where lotto_id=".$lotto_id." and uperid".$acl."=".$userid." and bettype=36 and numbers='".$numbers."' and rame=0" );
}
else if ( $acl == 5 )
{
    $rs = getdb( "select sum(amount*percent4/100) from bet where user_id in (".$users.") and lotto_id=".$lotto_id." and bettype=36 and numbers='".$numbers."' and rame=0" );
    $rs1 = getdb( "select sum(amount*percent".$acl."/100) from bet2 where lotto_id=".$lotto_id." and uperid".$acl."=".$userid." and bettype=36 and numbers='".$numbers."' and rame=0" );
}
$usedamount2 = $rs->f[0] ? $rs->f[0] : 0;
if ( 2 < $acl )
{
    $usedamount2 += $rs1->f[0] ? $rs1->f[0] : 0;
    $rs1->close();
}
$rs->close();
$rs = getdb( "select sum(amount) from bet2 where user_id=".$userid." and lotto_id=".$lotto_id." and bettype=36 and numbers='".$numbers."' and rame=0" );
$usedamount1 = $rs->f[0] ? $rs->f[0] : 0;
$rs->close();
$usemaxamount = $curruser['usemaxamount'];
if ( $money < $betmin )
{
    err( "低於最小注额！" );
}
else if ( $amount1 < $money )
{
    err( "超过单注限额！" );
}
else if ( $curruser['amount'] < 0 + $usedamount )
{
    err( "超过信用额度！" );
}
else if ( $maxnts < $money )
{
    err( "超过下注金额！" );
}
else if ( $usedamount2 < $money + $usedamount1 )
{
    err( "超过总下注额！" );
}
else if ( $usemaxamount && $curruser['maxamount'] < $money + $usedamount )
{
    err( "超过本期限额！" );
}
else if ( $stopbet <= date( "Hi" ) )
{
    err( "已经截止下注！" );
}
$ratetype = 1;
$raters = getdb( "select * from rate36 where lotto_id=".$lotto_id." and disk=1 and subid=".$subid );
if ( $raters->eof )
{
    err( "系统目前维护中，请稍後！" );
}
$maxRate = 0;
$minRate = 999;
$l = 0;
while ( $l < count( $num ) )
{
    $minRate = $raters->f["r".$num[$l]] < $minRate ? $raters->f["r".$num[$l]] : $minRate;
    $maxRate = $maxRate < $raters->f["r".$num[$l]] ? $raters->f["r".$num[$l]] : $maxRate;
    if ( $minRate == 0 || $minRate == 999 )
    {
        account_err( "连尾中的 ".$subid_count."尾 没有开放下注！" );
    }
    ++$l;
}
$stime = systime();
save2bet2( $userid, $acl, $previd, $bettype, $subid, $num, $money, $minRate, $u2, $u3, $u4, $u5, $stime );
echo "<p>走飞成功！</p>";
echo "<p><input type=button value='回上一页' onclick='history.back()'></p>";
echo "<script>";
echo "setTimeout(\"location.href='op.php?op=agent&fp=bnow36'\",30000);";
echo "</script>";
?>